package com.aoyy.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import com.aoyy.model.Board;
import com.aoyy.service.BoardService;
import com.aoyy.util.Page;
import com.aoyy.constant.Constants;
/**===================================================================================================
 * Board controller <code>BoardController</code> is to list, add, delete, update board, and more. 
 * @author this author
 * @date 2017-08-11
 * =================================================================================================== */
@Controller
public class BoardController extends BaseController {
	@Autowired
	private BoardService boardService;
	
	// Board List
	@RequestMapping(value="/board/boardList", method={RequestMethod.GET}) 
	public String toBoardList(HttpServletRequest request, @RequestParam(value="pageNo", required=false) Integer pageNo) {
		pageNo = (pageNo == null) ? 1 : pageNo;
		Page pagedBoards = boardService.pagedQuery(pageNo, Constants.PAGE_SIZE);
		request.setAttribute("pagedBoards", pagedBoards);
		return "/board/boardList";
	}
	
	// Board detail
	@RequestMapping(value="/board/boardDetail/{boardId}", method={RequestMethod.GET}) 
	public ModelAndView toBoardDetail(@PathVariable Integer boardId) {
		ModelAndView view = new ModelAndView();
		view.addObject("board", boardService.queryBoardByBoardId(boardId));
		view.setViewName("/board/boardDetail");
		return view;
	}
	
	// To add board
	@RequestMapping(value="/board/addBoard", method={RequestMethod.GET})
	public String toAddBoard() {
	    return "/board/addBoard";	
	}
	
	// Add board
	@RequestMapping(value="/board/addBoard", method={RequestMethod.POST}) 
	public ModelAndView addBoard(Board board) {
		ModelAndView view = new ModelAndView();
        view.setViewName("/board/addBoard");
        if (board.getBoardName() == null || board.getBoardName().isEmpty()) {
           view.addObject("message", "Name is required.");
           return view;
        } 
        if (board.getBoardName().length() > 255) {
        	view.addObject("message", "Name is required and no more than 255 characters.");
        	return view;
        }
        Board board2 = boardService.queryBoardByBoardName(board.getBoardName());
        if (board2 != null) {
           view.addObject("message", "The board using this board name exists!");
           return view;
        }
        if (board.getDescription() == null || board.getDescription().isEmpty()) {
        	view.addObject("message", "Description is required.");
        	return view;
        }
        boardService.save(board);
        view.addObject("message", "Add Board Success!");
	    return view;
	}
	
	// To update board
	@RequestMapping(value="/board/updateBoard-{boardId}", method={RequestMethod.GET})
	public String toUpdateBoard(HttpServletRequest request, @PathVariable Integer boardId) {
		Board board = boardService.queryBoardByBoardId(boardId);
		request.setAttribute("board", board);
		return "/board/updateBoard";
	}
	
	// Update board
	@RequestMapping(value="/board/updateBoard-{boardId}", method={RequestMethod.POST})
	public ModelAndView updateBoard(@PathVariable Integer boardId, Board board) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/board/updateBoard");
		if (board.getBoardName() == null || board.getBoardName().isEmpty()) {
	        view.addObject("message", "Name is required.");
	        return view;
	    } 
	    if (board.getBoardName().length() > 255) {
	    	view.addObject("message", "Name is required and no more than 255 characters.");
	        return view;
	    }
	    Board board2 = boardService.queryOtherWithSameName(board);
		if (board2 != null) {
		   view.addObject("message", "The Name is being used by other board!");
		   return view;
		}
		if (board.getDescription() == null || board.getDescription().isEmpty()) {
        	view.addObject("message", "Description is required.");
        	return view;
        }
		boardService.update(board);
		view.addObject("message", "Update Board Success!");
		return view;
	}
	
	// To delete board
	@RequestMapping(value="/board/deleteBoard-{boardId}", method={RequestMethod.GET})
	public String toDeleteBoard(HttpServletRequest request, @PathVariable Integer boardId) {
		Board board = boardService.queryBoardByBoardId(boardId);
		request.setAttribute("board", board);
		request.setAttribute("message", "Do you confirm to delete this board?");
		return "/board/deleteBoard";
	}
	
	// Delete board
	@RequestMapping(value="/board/deleteBoard-{boardId}", method={RequestMethod.POST})
	public ModelAndView deleteBoard(@PathVariable Integer boardId, Board board) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/board/deleteBoard");
		Board board2 = boardService.queryBoardByBoardId(board.getBoardId());
		if (board2 == null) {
			view.addObject("message", "The board has been deleted!");
			return view;
		}
		boardService.delete(board2);
		view.addObject("message", "Delete Board Success!");
		return view;
	}

}
